package com.jingan.nursinghome.regulatory.dialog;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.http.Http;
import com.jingan.nursinghome.regulatory.view.MyActionBar;
import com.jingan.nursinghome.regulatory.view.SpannerChildView;
import com.jingan.nursinghome.regulatory.view.SpannerChildView2;
import com.jingan.nursinghome.regulatory.view.SpannerView;
import com.jingan.nursinghome.regulatory.view.liView;
import com.jingan.nursinghome.regulatory.view.ryView;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.dialog.DateDialog;
import cn.android.support.v7.lib.sin.crown.dialog.ProgressDailog;
import cn.android.support.v7.lib.sin.crown.utils.UtilPopuWindow;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;
import cn.android.support.v7.lib.sin.crown.utils.UtilTime;

/**
 * Created by 彭治铭 on 2017/9/29.
 */

public class CreateMicrometerTableDailog extends BaseDialog {

    private CreateMicrometerTableDailog(BaseActivity activity) {
        init(activity, R.layout.dialog_create_micrometertablle2, false, false);
        window.setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_right);
        BaseApplication.getInstance().setStatusBarDrak(window, true);
        initUI();
        adapterUI();
    }

    MyActionBar myActionBar;
    TextView appraise;
    ryView txt_ry;

    //liView timu, jly, tm, ry, qt; //timu 题目类型
    liView jly, tm, ry, qt;

    EditText edit_qt;//其他
    SpannerView txt_timu, txt_jly, txt_tm, txt_ry1, txt_ry2;
    PopupWindow timupopu, jlypopu, ry1popu, ry2popu;//敬老院弹窗

//    ScrollView myscrollview;

    private void initUI() {
//        myscrollview = (ScrollView) findViewById(R.id.myscrollview);

        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "新建千分表巡检");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                dismiss();
            }
        });
//        timu = (liView) findViewById(R.id.timu);
//        timu.setLeftAndInfo("图标/题目类型.png", "题目类型");
        jly = (liView) findViewById(R.id.jly);
        jly.setLeftAndInfo("图标/敬老院.png", "养老机构");
        tm = (liView) findViewById(R.id.tm);
        tm.setLeftAndInfo("图标/时间.png", "时间");
        ry = (liView) findViewById(R.id.ry);
        ry.setLeftAndInfo("图标/主要沟通人员.png", "主要沟通人员");
        qt = (liView) findViewById(R.id.qt);
        qt.setLeftAndInfo("图标/其他.png", "其他");
        txt_ry = (ryView) findViewById(R.id.txt_ry);
        appraise = (TextView) findViewById(R.id.appraise);
        appraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //开始评分
            }
        });
        edit_qt = (EditText) findViewById(R.id.edit_qt);
//        txt_timu = (SpannerView) findViewById(R.id.txt_timu);
        txt_jly = (SpannerView) findViewById(R.id.txt_jly);
        txt_jly.setInfo("请选择千分表巡检的养老机构");
        txt_tm = (SpannerView) findViewById(R.id.txt_tm);
        if (MM < 10) {
            M = "0" + MM;
        } else {
            M = MM + "";
        }
        if (dd < 10) {
            D = "0" + dd;
        } else {
            D = dd + "";
        }
        txt_tm.setInfo(yyyy + "-" + M + "-" + D);
        txt_ry1 = (SpannerView) findViewById(R.id.txt_ry1);
        txt_ry1.setInfo("参与人员");
        txt_ry2 = (SpannerView) findViewById(R.id.txt_ry2);
        txt_ry2.setInfo("主管部门人员");

        //题目类型
        //txt_timu.setInfo("静安区");
//        if (MicrometerTable.getInstance().getTimuTypes().size() > 0) {
//            txt_timu.setInfo(MicrometerTable.getInstance().getTimuTypes().get(0).getQuname());
//        } else {
//            txt_timu.setInfo("静安区");
//        }

//        txt_timu.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (timupopu == null) {
//                    SpannerChildView spannerChildView = new SpannerChildView(jly.getContext());
//                    List<String> infos = new ArrayList<String>();
////                    infos.add("静安");
////                    infos.add("闸北");
//
//                    for (int i = 0; i < MicrometerTable.getInstance().getTimuTypes().size(); i++) {
//                        infos.add(MicrometerTable.getInstance().getTimuTypes().get(i).getQuname());
//                    }
//
//                    spannerChildView.setInfos(txt_timu, infos, 2, new BaseListenner<String>() {
//                        @Override
//                        public void onResult(String result) {
//                            txt_timu.setInfo(result);
//                        }
//                    });
//                    timupopu = UtilPopuWindow.getInstance().showPopuWindow(spannerChildView, cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_alpha);
//                    timupopu.showAsDropDown(txt_timu);
//                } else {
//                    timupopu.showAsDropDown(txt_timu);
//                }
//            }
//        });


        //敬老院
        txt_jly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (jlypopu == null) {
                    SpannerChildView spannerChildView = new SpannerChildView(jly.getContext());
                    List<String> infos = new ArrayList<String>();
                    for (int i = 0; i < MicrometerTable.getInstance().getJlys().size(); i++) {
                        infos.add(MicrometerTable.getInstance().getJlys().get(i).getName());
                    }
//                    infos.add("上海闸北区东风芷江养老院");
//                    infos.add("上海共康养老院");
//                    infos.add("上海豪锦粤秀养老院");
//                    infos.add("上海闸北区城建敬老院");
//                    infos.add("上海闸北区宝山路街道敬老院");
//                    infos.add("上海闸北区馨苑敬老院");
//                    infos.add("上海市静安区孙克仁老年福利院");
//                    infos.add("上海静安区蝴蝶湾敬老院");
//                    infos.add("上海生乐养老院");
//                    infos.add("上海闸北区共和新路街道敬老院");
//                    infos.add("上海闸北区沁怡敬老院");
//                    infos.add("上海闸北区教工敬老院");
//                    infos.add(" 上海闸北区天目西社区敬老院");
//                    infos.add("上海闸北区北站街道敬老院");
//                    infos.add("上海闸北区田泽文华敬老院");
//                    infos.add("上海闸北区一家人家敬老院");
//                    infos.add("上海闸北区社会福利院");
//                    infos.add("上海闸北区彭盛敬老院");
//                    infos.add("上海闸北区皓馨养老院");
//                    infos.add("上海闸北区彭浦新村老年公寓");
//                    infos.add("上海闸北区悦欣敬老院");
//                    infos.add("上海豪斯达敬老院");
//                    infos.add("上海闸北之星养老院");
//                    infos.add("上海新开元颐养院");
//                    infos.add("上海闸北区颐和养老院");
//                    infos.add("上海闸北郁金香颐养园");
//                    infos.add("上海静安区曹家渡爱华养老院");
//                    infos.add("上海静安区金康敬老院");
//                    infos.add("上海静安区江宁颐美敬老院");
//                    infos.add("上海静安区曹家渡街道敬老院");
//                    infos.add("上海静安老年公寓");
//                    infos.add("上海市静安区恒裕曹家渡老年福利院");
//                    infos.add("上海和养宝华养护院");
//                    infos.add("上海和养彭浦养护院");
//                    infos.add("上海市静安区乐宁老年福利院");
//                    infos.add("上海静安区日月星养老院");
//                    infos.add("上海和养临汾养护院");
//                    infos.add("上海闸北区彭浦新村第二老年公寓");
                    spannerChildView.setInfos(txt_jly, infos, (int) (txt_jly.getResources().getDisplayMetrics().heightPixels - txt_jly.getY() - txt_jly.getHeight()) / txt_jly.getHeight(), new BaseListenner<String>() {
                        @Override
                        public void onResult(String result) {
                            jlyName = result;
                            txt_jly.setInfo(result);
                        }
                    });
                    jlypopu = UtilPopuWindow.getInstance().showPopuWindow(spannerChildView, cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_alpha);
                    jlypopu.showAsDropDown(txt_jly);
                    spannerChildView.setPopupWindow(jlypopu);
                } else {
                    jlypopu.showAsDropDown(txt_jly);
                }
            }
        });

        //时间选择器
        txt_tm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DateDialog.getInstance(baseActivity, yyyy, MM, dd, new BaseListenner<List<Integer>>() {
                    @Override
                    public void onResult(List<Integer> result) {
                        yyyy = result.get(0);
                        MM = result.get(1);
                        dd = result.get(2);
                        if (MM < 10) {
                            M = "0" + MM;
                        } else {
                            M = MM + "";
                        }
                        if (dd < 10) {
                            D = "0" + dd;
                        } else {
                            D = dd + "";
                        }
                        txt_tm.setInfo(yyyy + "-" + M + "-" + D);
                    }
                }, true);
            }
        });
        txt_ry1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //参与人员
                if (ry1popu == null) {
                    SpannerChildView2 spannerChildView = new SpannerChildView2(txt_ry1.getContext());
                    spannerChildView.setIndex(0);
                    List<String> infos = new ArrayList<String>();
                    for (int i = 0; i < MicrometerTable.getInstance().getLinkups().size(); i++) {
                        if (MicrometerTable.getInstance().getLinkups().get(i).getLtype().equals("1")) {
                            infos.add(MicrometerTable.getInstance().getLinkups().get(i).getName());
                        }
                    }
                    spannerChildView.setInfos(txt_ry1, infos, 4, new BaseListenner<String>() {
                        @Override
                        public void onResult(String result) {
                            ry1 = result;
                            if (ry2.trim().equals("")) {
                                txt_ry.setInfo(ry1);
                            } else {
                                txt_ry.setInfo(ry1 + "、" + ry2);
                            }
                        }
                    });
                    ry1popu = UtilPopuWindow.getInstance().showPopuWindow(spannerChildView, cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_alpha);
                    ry1popu.showAsDropDown(txt_ry1);
                } else {
                    ry1popu.showAsDropDown(txt_ry1);
                }
            }
        });
        txt_ry2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //主管部门人员
                if (ry2popu == null) {
                    SpannerChildView2 spannerChildView = new SpannerChildView2(txt_ry2.getContext());
                    spannerChildView.setIndex(0);
                    List<String> infos = new ArrayList<String>();
                    for (int i = 0; i < MicrometerTable.getInstance().getLinkups().size(); i++) {
                        if (MicrometerTable.getInstance().getLinkups().get(i).getLtype().equals("2")) {
                            infos.add(MicrometerTable.getInstance().getLinkups().get(i).getName());
                        }
                    }
                    spannerChildView.setInfos(txt_ry2, infos, 4, new BaseListenner<String>() {
                        @Override
                        public void onResult(String result) {
                            ry2 = result;
                            if (ry1.trim().equals("")) {
                                txt_ry.setInfo(ry2);
                            } else {
                                txt_ry.setInfo(ry1 + "、" + ry2);
                            }
                        }
                    });
                    ry2popu = UtilPopuWindow.getInstance().showPopuWindow(spannerChildView, cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_alpha);
                    ry2popu.showAsDropDown(txt_ry2);
                } else {
                    ry2popu.showAsDropDown(txt_ry2);
                }
            }
        });
        appraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                time = txt_tm.getInfo();//日期时间
                names = txt_ry.getInfo();//主要沟通人员
                other = edit_qt.getText().toString().trim();//其他
//                String timu = txt_timu.getInfo();//题目类型
                if (jlyName == null) {
                    baseActivity.SnackbarMake("请先选择敬老院", true, findViewById(R.id.dialog_create_micrometertable_constraintLayout));
                } else if (names == null || names.trim().equals("")) {
                    baseActivity.SnackbarMake("沟通人员不可为空", true, findViewById(R.id.dialog_create_micrometertable_constraintLayout));
                } else {
                    //获取题目类型ID
//                    rid = MicrometerTable.getInstance().getTimuTypeID(txt_timu.getInfo().trim());

                    //获取千分表历史整改记录
//                    if (MicrometerTable.getInstance().getHostoryQFTopics().size() <= 0) {
                        //本来历史整改为空，从网络中获取
                        String oid = MicrometerTable.getInstance().getJlyID(jlyName);
                        //Log.e("test", "oid:\t" + oid);
                        final ProgressDailog progressDailog = ProgressDailog.getInstance(baseActivity);
                        Http.getInstance().getHostoryQFTopics(oid,rid, new BaseListenner() {
                            @Override
                            public void onResult(Object result) {
                                progressDailog.dismiss();
                                baseActivity.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        //跳转千分表评分系统
                                        MicrometerTableOSDialog.getInstance(baseActivity);
                                        dismiss();
                                    }
                                });
                            }
                        });
//                    } else {
//                        //跳转千分表评分系统
//                        MicrometerTableOSDialog.getInstance(baseActivity);
//                        dismiss();
//                    }

                }
            }
        });
    }

    public static String other;//其他
    public static String jlyName;//敬老院名称
    public static String rid="1";//题目类型1静安区 2闸北区
    public static String time;//时间
    public static String names;//主要沟通人员
    //public static int selectCount = 0;//参与人员 与 主管部分人员。最多可选五人

    String ry1 = "", ry2 = "";//選中人名單

    int yyyy = UtilTime.getInstance().getYear(null);
    int MM = UtilTime.getInstance().getMonth(null);
    int dd = UtilTime.getInstance().getDay(null);
    String M, D;

    private void adapterUI() {
        //ProportionUtils.getInstance().adapterScreen(baseActivity, findViewById(R.id.dialog_create_micrometertable_constraintLayout));
        ProportionUtils.getInstance().adapterScreen(baseActivity,findViewById(R.id.dialog_create_micrometertable_constraintLayout));
        ProportionUtils.getInstance().adapterTextView(appraise);
        ProportionUtils.getInstance().adapterView(findViewById(R.id.line1));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.line2));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.line3));
        ProportionUtils.getInstance().adapterTextView(edit_qt);
        ProportionUtils.getInstance().adapterView(txt_timu);
        ProportionUtils.getInstance().adapterView(txt_jly);
        ProportionUtils.getInstance().adapterView(txt_tm);
        ProportionUtils.getInstance().adapterView(txt_ry1);
        ProportionUtils.getInstance().adapterView(txt_ry2);
    }

    private static CreateMicrometerTableDailog createMicrometerTableDailog;

    public static CreateMicrometerTableDailog getInstance(BaseActivity activity) {
        if (isFinishing(createMicrometerTableDailog)) {
            createMicrometerTableDailog = new CreateMicrometerTableDailog(activity);
        } else {
            createMicrometerTableDailog.show();
        }
        return createMicrometerTableDailog;
    }

    @Override
    protected void listener() {
//        myscrollview.scrollTo(0, 0);
        MicrometerTable.getInstance().saveTopics();//保存千分表数据
        MicrometerTable.getInstance().topics.clear();//清除千分表数据
        MicrometerTable.getInstance().topicsHostoryQF.clear();//清楚千分表历史整改
    }

    @Override
    protected void recycleView() {
        if(edit_qt!=null){
            edit_qt.clearFocus();
        }
    }
}
